@@ -114,18 +114,6 @@ class Agent < ActiveRecord::Base |
||
| 114 | 114 |
end |
| 115 | 115 |
end |
| 116 | 116 |
|
| 117 |
- def last_event_at |
|
| 118 |
- @memoized_last_event_at ||= most_recent_event.try(:created_at) |
|
| 119 |
- end |
|
| 120 |
- |
|
| 121 |
- def set_default_schedule |
|
| 122 |
- self.schedule = default_schedule unless schedule.present? || cannot_be_scheduled? |
|
| 123 |
- end |
|
| 124 |
- |
|
| 125 |
- def unschedule_if_cannot_schedule |
|
| 126 |
- self.schedule = nil if cannot_be_scheduled? |
|
| 127 |
- end |
|
| 128 |
- |
|
| 129 | 117 |
def default_schedule |
| 130 | 118 |
self.class.default_schedule |
| 131 | 119 |
end |
@@ -11,7 +11,7 @@ |
||
| 11 | 11 |
# |
| 12 | 12 |
# It's strongly recommended to check this file into your version control system. |
| 13 | 13 |
|
| 14 |
-ActiveRecord::Schema.define(:version => 20131222211558) do |
|
| 14 |
+ActiveRecord::Schema.define(:version => 20131227000021) do |
|
| 15 | 15 |
|
| 16 | 16 |
create_table "agent_logs", :force => true do |t| |
| 17 | 17 |
t.integer "agent_id", :null => false |
@@ -37,6 +37,8 @@ ActiveRecord::Schema.define(:version => 20131222211558) do |
||
| 37 | 37 |
t.datetime "updated_at", :null => false |
| 38 | 38 |
t.text "memory", :limit => 2147483647 |
| 39 | 39 |
t.datetime "last_webhook_at" |
| 40 |
+ t.datetime "last_event_at" |
|
| 41 |
+ t.datetime "last_error_log_at" |
|
| 40 | 42 |
t.integer "keep_events_for", :default => 0, :null => false |
| 41 | 43 |
end |
| 42 | 44 |
|
@@ -457,7 +457,7 @@ describe Agent do |
||
| 457 | 457 |
end |
| 458 | 458 |
|
| 459 | 459 |
it "sets expires_at on created events" do |
| 460 |
- event = agents(:jane_weather_agent).create_event :payload => "hi!" |
|
| 460 |
+ event = agents(:jane_weather_agent).create_event :payload => { 'hi' => 'there' }
|
|
| 461 | 461 |
event.expires_at.should be_within(5).of(agents(:jane_weather_agent).keep_events_for.days.from_now) |
| 462 | 462 |
end |
| 463 | 463 |
end |
@@ -468,7 +468,7 @@ describe Agent do |
||
| 468 | 468 |
end |
| 469 | 469 |
|
| 470 | 470 |
it "does not set expires_at on created events" do |
| 471 |
- event = agents(:jane_website_agent).create_event :payload => "hi!" |
|
| 471 |
+ event = agents(:jane_website_agent).create_event :payload => { 'hi' => 'there' }
|
|
| 472 | 472 |
event.expires_at.should be_nil |
| 473 | 473 |
end |
| 474 | 474 |
end |
@@ -4,15 +4,15 @@ describe Agents::WebsiteAgent do |
||
| 4 | 4 |
before do |
| 5 | 5 |
stub_request(:any, /xkcd/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/xkcd.html")), :status => 200)
|
| 6 | 6 |
@site = {
|
| 7 |
- :name => "XKCD", |
|
| 8 |
- :expected_update_period_in_days => 2, |
|
| 9 |
- :type => "html", |
|
| 10 |
- :url => "http://xkcd.com", |
|
| 11 |
- :mode => :on_change, |
|
| 12 |
- :extract => {
|
|
| 13 |
- :url => {:css => "#comic img", :attr => "src"},
|
|
| 14 |
- :title => {:css => "#comic img", :attr => "title"}
|
|
| 15 |
- } |
|
| 7 |
+ 'name' => "XKCD", |
|
| 8 |
+ 'expected_update_period_in_days' => 2, |
|
| 9 |
+ 'type' => "html", |
|
| 10 |
+ 'url' => "http://xkcd.com", |
|
| 11 |
+ 'mode' => 'on_change', |
|
| 12 |
+ 'extract' => {
|
|
| 13 |
+ 'url' => {'css' => "#comic img", 'attr' => "src"},
|
|
| 14 |
+ 'title' => {'css' => "#comic img", 'attr' => "title"}
|
|
| 15 |
+ } |
|
| 16 | 16 |
} |
| 17 | 17 |
@checker = Agents::WebsiteAgent.new(:name => "xkcd", :options => @site) |
| 18 | 18 |
@checker.user = users(:bob) |
@@ -27,7 +27,7 @@ describe Agents::WebsiteAgent do |
||
| 27 | 27 |
|
| 28 | 28 |
it "should always save events when in :all mode" do |
| 29 | 29 |
lambda {
|
| 30 |
- @site[:mode] = :all |
|
| 30 |
+ @site['mode'] = 'all' |
|
| 31 | 31 |
@checker.options = @site |
| 32 | 32 |
@checker.check |
| 33 | 33 |
@checker.check |
@@ -35,7 +35,7 @@ describe Agents::WebsiteAgent do |
||
| 35 | 35 |
end |
| 36 | 36 |
|
| 37 | 37 |
it "should log an error if the number of results for a set of extraction patterns differs" do |
| 38 |
- @site[:extract][:url][:css] = "div" |
|
| 38 |
+ @site['extract']['url']['css'] = "div" |
|
| 39 | 39 |
@checker.options = @site |
| 40 | 40 |
@checker.check |
| 41 | 41 |
@checker.logs.first.message.should =~ /Got an uneven number of matches/ |
@@ -68,20 +68,20 @@ describe Agents::WebsiteAgent do |
||
| 68 | 68 |
it "parses CSS" do |
| 69 | 69 |
@checker.check |
| 70 | 70 |
event = Event.last |
| 71 |
- event.payload[:url].should == "http://imgs.xkcd.com/comics/evolving.png" |
|
| 72 |
- event.payload[:title].should =~ /^Biologists play reverse/ |
|
| 71 |
+ event.payload['url'].should == "http://imgs.xkcd.com/comics/evolving.png" |
|
| 72 |
+ event.payload['title'].should =~ /^Biologists play reverse/ |
|
| 73 | 73 |
end |
| 74 | 74 |
|
| 75 | 75 |
it "should turn relative urls to absolute" do |
| 76 | 76 |
rel_site = {
|
| 77 |
- :name => "XKCD", |
|
| 78 |
- :expected_update_period_in_days => 2, |
|
| 79 |
- :type => "html", |
|
| 80 |
- :url => "http://xkcd.com", |
|
| 81 |
- :mode => :on_change, |
|
| 82 |
- :extract => {
|
|
| 83 |
- :url => {:css => "#topLeft a", :attr => "href"},
|
|
| 84 |
- :title => {:css => "#topLeft a", :text => "true"}
|
|
| 77 |
+ 'name' => "XKCD", |
|
| 78 |
+ 'expected_update_period_in_days' => 2, |
|
| 79 |
+ 'type' => "html", |
|
| 80 |
+ 'url' => "http://xkcd.com", |
|
| 81 |
+ 'mode' => :on_change, |
|
| 82 |
+ 'extract' => {
|
|
| 83 |
+ 'url' => {'css' => "#topLeft a", 'attr' => "href"},
|
|
| 84 |
+ 'title' => {'css' => "#topLeft a", 'text' => "true"}
|
|
| 85 | 85 |
} |
| 86 | 86 |
} |
| 87 | 87 |
rel = Agents::WebsiteAgent.new(:name => "xkcd", :options => rel_site) |
@@ -89,28 +89,28 @@ describe Agents::WebsiteAgent do |
||
| 89 | 89 |
rel.save! |
| 90 | 90 |
rel.check |
| 91 | 91 |
event = Event.last |
| 92 |
- event.payload[:url].should == "http://xkcd.com/about" |
|
| 92 |
+ event.payload['url'].should == "http://xkcd.com/about" |
|
| 93 | 93 |
end |
| 94 |
- |
|
| 94 |
+ |
|
| 95 | 95 |
describe "JSON" do |
| 96 | 96 |
it "works with paths" do |
| 97 | 97 |
json = {
|
| 98 |
- :response => {
|
|
| 99 |
- :version => 2, |
|
| 100 |
- :title => "hello!" |
|
| 101 |
- } |
|
| 98 |
+ 'response' => {
|
|
| 99 |
+ 'version' => 2, |
|
| 100 |
+ 'title' => "hello!" |
|
| 101 |
+ } |
|
| 102 | 102 |
} |
| 103 | 103 |
stub_request(:any, /json-site/).to_return(:body => json.to_json, :status => 200) |
| 104 | 104 |
site = {
|
| 105 |
- :name => "Some JSON Response", |
|
| 106 |
- :expected_update_period_in_days => 2, |
|
| 107 |
- :type => "json", |
|
| 108 |
- :url => "http://json-site.com", |
|
| 109 |
- :mode => :on_change, |
|
| 110 |
- :extract => {
|
|
| 111 |
- :version => { :path => "response.version" },
|
|
| 112 |
- :title => { :path => "response.title" }
|
|
| 113 |
- } |
|
| 105 |
+ 'name' => "Some JSON Response", |
|
| 106 |
+ 'expected_update_period_in_days' => 2, |
|
| 107 |
+ 'type' => "json", |
|
| 108 |
+ 'url' => "http://json-site.com", |
|
| 109 |
+ 'mode' => 'on_change', |
|
| 110 |
+ 'extract' => {
|
|
| 111 |
+ 'version' => {'path' => "response.version"},
|
|
| 112 |
+ 'title' => {'path' => "response.title"}
|
|
| 113 |
+ } |
|
| 114 | 114 |
} |
| 115 | 115 |
checker = Agents::WebsiteAgent.new(:name => "Weather Site", :options => site) |
| 116 | 116 |
checker.user = users(:bob) |
@@ -118,30 +118,30 @@ describe Agents::WebsiteAgent do |
||
| 118 | 118 |
|
| 119 | 119 |
checker.check |
| 120 | 120 |
event = Event.last |
| 121 |
- event.payload[:version].should == 2 |
|
| 122 |
- event.payload[:title].should == "hello!" |
|
| 121 |
+ event.payload['version'].should == 2 |
|
| 122 |
+ event.payload['title'].should == "hello!" |
|
| 123 | 123 |
end |
| 124 | 124 |
|
| 125 | 125 |
it "can handle arrays" do |
| 126 | 126 |
json = {
|
| 127 |
- :response => {
|
|
| 128 |
- :data => [ |
|
| 129 |
- { :title => "first", :version => 2 },
|
|
| 130 |
- { :title => "second", :version => 2.5 }
|
|
| 131 |
- ] |
|
| 132 |
- } |
|
| 127 |
+ 'response' => {
|
|
| 128 |
+ 'data' => [ |
|
| 129 |
+ {'title' => "first", 'version' => 2},
|
|
| 130 |
+ {'title' => "second", 'version' => 2.5}
|
|
| 131 |
+ ] |
|
| 132 |
+ } |
|
| 133 | 133 |
} |
| 134 | 134 |
stub_request(:any, /json-site/).to_return(:body => json.to_json, :status => 200) |
| 135 | 135 |
site = {
|
| 136 |
- :name => "Some JSON Response", |
|
| 137 |
- :expected_update_period_in_days => 2, |
|
| 138 |
- :type => "json", |
|
| 139 |
- :url => "http://json-site.com", |
|
| 140 |
- :mode => :on_change, |
|
| 141 |
- :extract => {
|
|
| 142 |
- :title => { :path => "response.data[*].title" },
|
|
| 143 |
- :version => { :path => "response.data[*].version" }
|
|
| 144 |
- } |
|
| 136 |
+ 'name' => "Some JSON Response", |
|
| 137 |
+ 'expected_update_period_in_days' => 2, |
|
| 138 |
+ 'type' => "json", |
|
| 139 |
+ 'url' => "http://json-site.com", |
|
| 140 |
+ 'mode' => 'on_change', |
|
| 141 |
+ 'extract' => {
|
|
| 142 |
+ :title => {'path' => "response.data[*].title"},
|
|
| 143 |
+ :version => {'path' => "response.data[*].version"}
|
|
| 144 |
+ } |
|
| 145 | 145 |
} |
| 146 | 146 |
checker = Agents::WebsiteAgent.new(:name => "Weather Site", :options => site) |
| 147 | 147 |
checker.user = users(:bob) |
@@ -152,28 +152,28 @@ describe Agents::WebsiteAgent do |
||
| 152 | 152 |
}.should change { Event.count }.by(2)
|
| 153 | 153 |
|
| 154 | 154 |
event = Event.all[-1] |
| 155 |
- event.payload[:version].should == 2.5 |
|
| 156 |
- event.payload[:title].should == "second" |
|
| 155 |
+ event.payload['version'].should == 2.5 |
|
| 156 |
+ event.payload['title'].should == "second" |
|
| 157 | 157 |
|
| 158 | 158 |
event = Event.all[-2] |
| 159 |
- event.payload[:version].should == 2 |
|
| 160 |
- event.payload[:title].should == "first" |
|
| 159 |
+ event.payload['version'].should == 2 |
|
| 160 |
+ event.payload['title'].should == "first" |
|
| 161 | 161 |
end |
| 162 | 162 |
|
| 163 | 163 |
it "stores the whole object if :extract is not specified" do |
| 164 | 164 |
json = {
|
| 165 |
- :response => {
|
|
| 166 |
- :version => 2, |
|
| 167 |
- :title => "hello!" |
|
| 168 |
- } |
|
| 165 |
+ 'response' => {
|
|
| 166 |
+ 'version' => 2, |
|
| 167 |
+ 'title' => "hello!" |
|
| 168 |
+ } |
|
| 169 | 169 |
} |
| 170 | 170 |
stub_request(:any, /json-site/).to_return(:body => json.to_json, :status => 200) |
| 171 | 171 |
site = {
|
| 172 |
- :name => "Some JSON Response", |
|
| 173 |
- :expected_update_period_in_days => 2, |
|
| 174 |
- :type => "json", |
|
| 175 |
- :url => "http://json-site.com", |
|
| 176 |
- :mode => :on_change |
|
| 172 |
+ 'name' => "Some JSON Response", |
|
| 173 |
+ 'expected_update_period_in_days' => 2, |
|
| 174 |
+ 'type' => "json", |
|
| 175 |
+ 'url' => "http://json-site.com", |
|
| 176 |
+ 'mode' => 'on_change' |
|
| 177 | 177 |
} |
| 178 | 178 |
checker = Agents::WebsiteAgent.new(:name => "Weather Site", :options => site) |
| 179 | 179 |
checker.user = users(:bob) |
@@ -181,8 +181,8 @@ describe Agents::WebsiteAgent do |
||
| 181 | 181 |
|
| 182 | 182 |
checker.check |
| 183 | 183 |
event = Event.last |
| 184 |
- event.payload[:response][:version].should == 2 |
|
| 185 |
- event.payload[:response][:title].should == "hello!" |
|
| 184 |
+ event.payload['response']['version'].should == 2 |
|
| 185 |
+ event.payload['response']['title'].should == "hello!" |
|
| 186 | 186 |
end |
| 187 | 187 |
end |
| 188 | 188 |
end |